﻿
<div id="easyDialogWrapper{{Id}}" class="easyDialog_wrapper" style="display: none; margin: 0px; width:1000px">
<div class="easyDialog_content">
<h4 id="easyDialogTitle" class="easyDialog_title" style="cursor: move;">
<a id="closeBtn" class="close_btn" title="关闭窗口" href="javascript:void(0)">×</a>
选择
</h4>
    <div class="easyDialog_text">
        <div id="easyDialog_text{{Id}}" style="float:left">

        </div>
        <div style="float:right">
            <table>
                <tbody id="SelectedItem{{Id}}">

                </tbody>
            </table>
        </div>
    </div>

    <div class="easyDialog_footer" style="clear:both">        
        <button id="easyDialogNoBtn{{Id}}" class="btn_normal">取消</button>
        <button id="easyDialogYesBtn{{Id}}" class="btn_highlight">确定</button>
<!--        <button id="easyDialogClear{{Id}}" class="btn_normal">清空</button>-->
    </div>
</div>
</div>    
<input id="{{Id}}" name="{{Id}}" type="text" value="{{ValueList}}" style="display: none"/>
<input id="SelectDialogForm_text{{Id}}" value="{{TextList}}" name="SelectDialogForm_text{{Id}}" type="text" readonly="readonly" />
<input id="SelectDialogForm_url{{Id}}" value="{{ListUrl}}" name="SelectDialogForm_url{{Id}}" type="text" style="display: none" />
<input id="SelectDialogForm_button{{Id}}" type="button" onclick="ClickSelect('{{Id}}', '{{MaxChoicesNum}}')" value="选择" />


<script  type="text/javascript">
    if (!Array.prototype.forEach) {
        Array.prototype.forEach = function (callback, thisArg) {
            var T, k;
            if (this == null) {
                throw new TypeError(" this is null or not defined");
            }
            var O = Object(this);
            var len = O.length >>> 0; // Hack to convert O.length to a UInt32  
            if ({}.toString.call(callback) != "[object Function]") {
                throw new TypeError(callback + " is not a function");
            }
            if (thisArg) {
                T = thisArg;
            }
            k = 0;
            while (k < len) {
                var kValue;
                if (k in O) {
                    kValue = O[k];
                    callback.call(T, kValue, k, O);
                }
                k++;
            }
        };
    }

    var controlId;

    //插入选择的项
    function InsertSelectItem(value,text,cid)
    {
        //alert("#SelectedItem" + controlId);
        $("#SelectedItem" + controlId).append("<tr id=select" + cid + "><td style='display: none'>" + value + "</td><td>" + text + "</td><td><input id=\"Button1\" type=\"button\" onclick='DeleteSelectItem(this)' value=\"删除\" /></td></tr>");

    }
    

    
    //选中改变事件
    function CheckboxChange(obj)
    {
        var tr = $(obj).parent().parent();
        var trid = tr.attr("id");
        trid = trid.split("tr-")[1];
        //alert(tr.find(".Remark2").text());
        var info = GetValueText(tr, trid);
        if (obj.checked) {
            // var id
            
            var trList = $("#SelectedItem" + controlId + " tr");
            var num = trList.length+1;
            if (maxChoicesNum>0 && num > maxChoicesNum) {
                alert("最多选择：" + maxChoicesNum + "个");
                obj.checked=false;
                return;
            }
            
            
            InsertSelectItem(info[0], info[1], trid);

            valueList = valueList + info[0] + "|";
            textList = textList + info[1] + "|";
        }
        else
        {
            $("#select" + trid).remove();
            valueList = valueList.replace("|" + info[0] + "|", "|");
            textList = textList.replace("|" + info[1] + "|", "|");
        }
    
    }
  

    function GetListCallBack() {
        $("#easyDialog_text" + controlId + " table tbody tr").each(function () {
            $(this).find("td:first").before("<td><input id=\"Checkbox1\" type=\"checkbox\" onclick=\"CheckboxChange(this)\" /></td>");
        });
        //$("#tr-" + list[i]).find("input[type='checkbox']").attr("checked", true);
        InitData();
    }
    ///删除已经选中的项
    function DeleteSelectItem(obj) {
        //alert(obj.id);
        var text = $(obj).parent().prev().text();
        textList = textList.replace("|" + text + "|", "|");

        var tr = $(obj).parent().parent();
        tr.remove()     ;
        var id = tr.attr("id").split("select")[1];
        //alert(id);
        $("#tr-" + id).find("input[type='checkbox']").attr("checked", false);
        valueList = valueList.replace("|" + id + "|", "|");
    }

    function GetValueText(tr,cid) {
        var value;
        var text;
        if ('{{value}}' == '') {
            //alert(this.id);
            value = cid;
        }
        else {
            value = tr.find("." + "{{value}}").text(); //tdList.eq(parseInt('{{value}}') + 1).text();
        }
        if ('{{text}}' == '') {
            text = cid;
        }
        else {
           // alert(tdList.length);
            text = tr.find("." + "{{text}}").text(); //tdList.eq(parseInt('{{text}}') + 1).text();
        }
        return [value, text];
    }


      //加载选中的项
    function InitData() {
        var selectNum = $("#SelectedItem" + controlId+ " tr").length;
        //var valueList = $("#{{Id}}").val();
        if (valueList != "") {
            if ('{{value}}' == '') {
                var list = valueList.split("|");
                var num = list.length;
                if (num > 0) {
                    //var textList = $("#SelectDialogForm_text{{Id}}").val();
                    var tlist=textList.split("|");
                    for (var i = 0; i < list.length; i++) {
                        if (list[i] != "") {
                            if (selectNum==0) {
                                InsertSelectItem(list[i], tlist[i],list[i]);
                            }                          
                            $("#tr-" + list[i]).find("input[type='checkbox']").attr("checked", true);
                        }
                    }
                }
            }
        }       
    }
    
    var valueList = "|";
    var textList = "|";
    var maxChoicesNum;
    function ClickSelect(cid, maxNum) {
        controlId = cid;
        maxChoicesNum = maxNum;

        valueList = $("#" + controlId).val();
        textList = $("#SelectDialogForm_text" + controlId).val();
        if (valueList == "") {
            valueList = "|";
            textList = "|";
        }
        var url = $("#SelectDialogForm_url"+controlId).val();
        $.get(url, function (data) {
            $("#easyDialog_text" + controlId).html(data);

            easyDialog.open({
                container: "easyDialogWrapper" + controlId,
                overlay: true
            });

            if (typeof (DataPaging_Operat) != "undefined") {
                DataPaging_Operat.RefreshCallBack = function () {
                    GetListCallBack();
                };
            }
            ;
            GetListCallBack();
        });




        var ds_IsRadio = false;





        //function Initialization() {
        //    if (ds_IsRadio) {
        //        valueList = "";
        //        textList = "";
        //    } else {
        //        valueList = "|";
        //        textList = "|";
        //    }
        //}
        if (ds_IsRadio) {

        } else {
            //Initialization();
        }

        $("#easyDialogYesBtn" + controlId).click(function () {

            $("#" + controlId).val(valueList);
            $("#SelectDialogForm_text" + controlId).val(textList);
            //Initialization();
            easyDialog.close();
        });
        $("#easyDialogNoBtn" + controlId).click(function () {
            easyDialog.close();
        });
        $("#easyDialogClear" + controlId).click(function () {
            $("#" + controlId).val("");
            $("#SelectDialogForm_text" + controlId).val("");
        });

  }



</script>